package com.dora.pattern.strategy.enumration;

import com.dora.pattern.strategy.Stock;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 排名枚举
 *
 * @author hanchen
 * @date 2024/03/14
 */
public enum RankEnum {
    // 以下三个为策略实例
    HighPrice {
        @Override
        public List<Stock> sort(List<Stock> source) {
            return source.stream()
                    .sorted(Comparator.comparing(Stock::getPrice).reversed())
                    .collect(Collectors.toList());
        }
    },
    LowPrice {
        @Override
        public List<Stock> sort(List<Stock> source) {
            return source.stream()
                    .sorted(Comparator.comparing(Stock::getPrice))
                    .collect(Collectors.toList());
        }
    },
    HighRise {
        @Override
        public List<Stock> sort(List<Stock> source) {
            return source.stream()
                    .sorted(Comparator.comparing(Stock::getRise).reversed())
                    .collect(Collectors.toList());
        }
    };

    /**
     * 排序 这里定义了策略接口
     *
     * @param source 源
     * @return {@link List}<{@link Stock}>
     */
    public abstract List<Stock> sort(List<Stock> source);
}
